package com.abl.common.mybatis.model;


import com.abl.common.mybatis.strengthen.DbFillKit;
import com.abl.common.util.coll.MapKit;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用于数据库传输数据的实体
 */
public class Dnt extends JSONObject {

    public Dnt() {
    }

    //
    public static Dnt create() {
        return new Dnt();
    }

    public static Dnt create(Map map) {
        Dnt dnt = new Dnt();
        dnt.set(map);
        return dnt;
    }

    public static Dnt create(Dnt dnt) {
        Dnt ret = new Dnt();
        ret.set(dnt);
        return ret;
    }

    public static Dnt create(JSONObject json) {
        Dnt ret = new Dnt();
        Set<String> keys = json.keySet();
        for (String key : keys) {
            ret.set(key, json.get(key));
        }
        return ret;
    }

    public static Dnt create(String key, Object value) {
        return new Dnt().set(key, value);
    }

    public static List<Dnt> create(List<Object> list) {
        return JSON.parseArray(JSON.toJSONString(list), Dnt.class);
    }

    public static List<Dnt> create(JSONArray list) {
        return JSON.parseArray(list.toString(), Dnt.class);
    }

    //
    //
    //
    public static Dnt createInsert() {
        Dnt dnt = new Dnt();
        DbFillKit.fillInsert(dnt);
        return dnt;
    }

    public static Dnt createInsert(Map map) {
        Dnt dnt = new Dnt();
        dnt.set(map);
        DbFillKit.fillInsert(dnt);
        return dnt;
    }

    public static Dnt createInsert(Dnt dnt) {
        Dnt ret = new Dnt();
        ret.set(dnt);
        DbFillKit.fillInsert(dnt);
        return ret;
    }

    /**
     * 将参数中的数据，转存到实体类中
     *
     * @Param<JSONObject> param 原始参数
     */
    public static Dnt createInsert(JSONObject param) {
        Dnt dnt = new Dnt();
        Set<String> keys = param.keySet();
        for (String key : keys) {
            dnt.set(key, param.get(key));
        }
        DbFillKit.fillInsert(dnt);
        return dnt;
    }

    /**
     * 将参数中的数据，转存到实体类中
     *
     * @param param 原始参数
     * @param keys  转存key数组
     */
    public static Dnt createInsert(JSONObject param, Collection<String> keys) {
        Dnt dnt = create(param);
        MapKit.set(param, keys);
        DbFillKit.fillInsert(dnt);
        return dnt;
    }

    /**
     * 将参数中的数据，转存到实体类中
     *
     * @param param 原始参数
     * @param keys  动态数组
     */
    public static Dnt createInsert(JSONObject param, String... keys) {
        Dnt dnt = create(param);
        MapKit.set(param, keys);
        DbFillKit.fillInsert(dnt);
        return dnt;
    }

    public static Dnt createInsert(String key, Object value) {
        Dnt dnt = new Dnt().set(key, value);
        DbFillKit.fillInsert(dnt);
        return dnt;
    }


    public static List<Dnt> createInsert(List<Object> list) {
        List<Dnt> dnts = JSON.parseArray(JSON.toJSONString(list), Dnt.class);
        DbFillKit.fillInsert(dnts);
        return dnts;
    }

    public static List<Dnt> createInsert(JSONArray list) {
        List<Dnt> dnts = JSON.parseArray(list.toString(), Dnt.class);
        DbFillKit.fillInsert(dnts);
        return dnts;
    }

    //
    //
    //
    public static Dnt createUpdate() {
        Dnt dnt = new Dnt();
        DbFillKit.fillUpdate(dnt);
        return dnt;
    }

    public static Dnt createUpdate(Map map) {
        Dnt dnt = new Dnt();
        dnt.set(map);
        DbFillKit.fillUpdate(dnt);
        return dnt;
    }

    public static Dnt createUpdate(Dnt dnt) {
        Dnt ret = new Dnt();
        ret.set(dnt);
        DbFillKit.fillUpdate(dnt);
        return ret;
    }

    public static Dnt createUpdate(JSONObject json) {
        Dnt dnt = new Dnt();
        Set<String> keys = json.keySet();
        for (String key : keys) {
            dnt.set(key, json.get(key));
        }
        DbFillKit.fillUpdate(dnt);
        return dnt;
    }

    public static Dnt createUpdate(String key, Object value) {
        Dnt dnt = new Dnt().set(key, value);
        DbFillKit.fillUpdate(dnt);
        return dnt;
    }

    public static List<Dnt> createUpdate(List<Object> list) {
        List<Dnt> dnts = JSON.parseArray(JSON.toJSONString(list), Dnt.class);
        DbFillKit.fillUpdate(dnts);
        return dnts;
    }

    public static List<Dnt> createUpdate(JSONArray list) {
        List<Dnt> dnts = JSON.parseArray(list.toString(), Dnt.class);
        DbFillKit.fillUpdate(dnts);
        return dnts;
    }


    /**
     * 将参数中的数据，转存到实体类中
     *
     * @param keys  转存key数组
     * @param param 原始参数
     */
    public static Dnt createUpdate(JSONObject param, Collection<String> keys) {
        Dnt dnt = create(param);
        MapKit.set(param, keys);
        DbFillKit.fillUpdate(dnt);
        return dnt;
    }

    /**
     * 将参数中的数据，转存到实体类中
     *
     * @param keys  逗号分隔字符串
     * @param param 原始参数
     */
    public static Dnt createUpdate(JSONObject param, String... keys) {
        Dnt dnt = create(param);
        MapKit.set(param, keys);
        DbFillKit.fillUpdate(dnt);
        return dnt;
    }

    //
    //
    //
    public static Dnt createDelete() {
        Dnt dnt = new Dnt();
        DbFillKit.fillDelete(dnt);
        return dnt;
    }

    public static Dnt createDelete(Map map) {
        Dnt dnt = new Dnt();
        dnt.set(map);
        DbFillKit.fillDelete(dnt);
        return dnt;
    }

    public static Dnt createDelete(Dnt dnt) {
        Dnt ret = new Dnt();
        ret.set(dnt);
        DbFillKit.fillDelete(dnt);
        return ret;
    }

    public static Dnt createDelete(JSONObject json) {
        Dnt dnt = new Dnt();
        Set<String> keys = json.keySet();
        for (String key : keys) {
            dnt.set(key, json.get(key));
        }
        DbFillKit.fillDelete(dnt);
        return dnt;
    }

    public static Dnt createDelete(String key, Object value) {
        Dnt dnt = new Dnt().set(key, value);
        DbFillKit.fillDelete(dnt);
        return dnt;
    }

    public static List<Dnt> createDelete(List<Object> list) {
        List<Dnt> dnts = JSON.parseArray(JSON.toJSONString(list), Dnt.class);
        DbFillKit.fillDelete(dnts);
        return dnts;
    }

    public static List<Dnt> createDelete(JSONArray list) {
        List<Dnt> dnts = JSON.parseArray(list.toString(), Dnt.class);
        DbFillKit.fillDelete(dnts);
        return dnts;
    }


    //
    //
    //
    public Dnt set(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public Dnt set(Map map) {
        super.putAll(map);
        return this;
    }

    public Dnt set(Dnt dnt) {
        super.putAll(dnt);
        return this;
    }

    public Dnt set(JSONObject json) {
        super.putAll(json);
        return this;
    }


    /**
     * 只将param中key为useKeys中的数据set到Ent对象中
     */
    public Dnt set(String[] keyS, Map<String, String> param) {
        for (String useKey : keyS) {
            if (param.get(useKey) != null) {
                set(useKey, param.get(useKey));
            }
        }
        return this;
    }

    public Dnt delete(Object key) {
        super.remove(key);
        return this;
    }

}
